package com.ml.mall.service.notifation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.notifation.NotifationEditDto;
import com.ml.mall.bean.req.notifation.NotifationNewDto;
import com.ml.mall.bean.req.notifation.NotifationQueryDto;
import com.ml.mall.bean.req.notifation.NotifationQueryOneDto;
import com.ml.mall.bean.res.notifation.NotifationQueryInfoRes;
import com.ml.mall.bean.res.notifation.NotifationQueryRes;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.notifation.SysANotifation;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.notifation.SysANotifationMapper;
import com.ml.mall.model.MailMessage;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.notifation.SysANotifationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysANotifationServiceImpl extends ServiceImpl<SysANotifationMapper, SysANotifation> implements SysANotifationService {

    @Autowired
    private SysANotifationMapper sysANotifationMapper;


    @Override
    public com.ml.mall.bean.page.Page<NotifationQueryRes> queryPageNotifation(NotifationQueryDto dto) {
        SysUser currentUser = JwtUtil.getCurrentUser();

        Page<SysANotifation> page = new Page<>(dto.getPageIndex(),dto.getPageNum());
        LambdaQueryWrapper<SysANotifation> wrapper = new LambdaQueryWrapper<>();

        //管理员查询管理员通知与本公司通知，非管理员查询本公司通知
        Long currentCompanyId = currentUser.getCompanyId();
        if(CommonConsts.ROLE_TYPE_ADMIN.equals(currentUser.getRoleType())){
            wrapper.and(w1 -> {
                w1.eq(SysANotifation::getNotifationType, CommonConsts.NOTIFICATION_TYPE_ADMIN)
                      .or(w -> w
                              .eq(null != currentCompanyId, SysANotifation::getNotifationType, CommonConsts.NOTIFICATION_TYPE_COMPANY)
                              .eq(null != currentCompanyId, SysANotifation::getCompanyId, currentCompanyId)
                      );
            });
        }else{
            wrapper.eq(SysANotifation::getNotifationType, CommonConsts.NOTIFICATION_TYPE_COMPANY)
                   .eq(SysANotifation::getCompanyId, currentCompanyId);
        }

        if(dto.getTime() != null){
            wrapper.ge(SysANotifation::getCreateAt, dto.getTime())
                   .lt(SysANotifation::getCreateAt, DateUtils.addDays(dto.getTime(), 1));
        }
        wrapper.orderByDesc(SysANotifation::getId);
        Page<SysANotifation> notifationIPage = sysANotifationMapper.selectPage(page, wrapper);
        List<NotifationQueryRes> notifationQueryResList = new ArrayList<>();
        for (SysANotifation notifation:notifationIPage.getRecords()) {
            NotifationQueryRes notifationQueryRes = new NotifationQueryRes();
            BeanUtils.copyProperties(notifation, notifationQueryRes);
            notifationQueryResList.add(notifationQueryRes);
        }
        com.ml.mall.bean.page.Page<NotifationQueryRes> resPage = new com.ml.mall.bean.page.Page<>(dto.getPageIndex(), dto.getPageNum());
        resPage.setTotal(Long.valueOf(notifationIPage.getTotal()).intValue());
        resPage.setRecords(notifationQueryResList);
        return resPage;
    }

    @Override
    public NotifationQueryInfoRes queryOneNotifation(NotifationQueryOneDto dto) {
        SysANotifation sysANotifation = sysANotifationMapper.selectById(dto.getId());
        NotifationQueryInfoRes notifationInfoRes = new NotifationQueryInfoRes();
        BeanUtils.copyProperties(sysANotifation,notifationInfoRes);
        return notifationInfoRes;
    }

    @Override
    public ApiRestResult createNotifation(NotifationNewDto dto) {
        SysANotifation sysANotifation = new SysANotifation();
        BeanUtils.copyProperties(dto,sysANotifation);
        int isAdd = sysANotifationMapper.insert(sysANotifation);
        if(isAdd < 1){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return ApiRestResult.ok();
    }

    @Override
    public ApiRestResult editNotifation(NotifationEditDto dto) {
        SysANotifation sysANotifation = new SysANotifation();
        BeanUtils.copyProperties(dto,sysANotifation);
        int isUpdate = sysANotifationMapper.updateById(sysANotifation);
        if(isUpdate < 1){
            throw new BizException(CommonEnum.UPDATE_ERROR);
        }
        return ApiRestResult.ok();
    }

    @Transactional
    @Override
    public ApiRestResult delNotifation(List<Long> ids) {
        sysANotifationMapper.deleteBatchIds(ids);
        return ApiRestResult.ok();
    }

    @Override
    public ApiRestResult notifyAdmin(MailMessage mailMessage) {
        NotifationNewDto dto = new NotifationNewDto();
        dto.setTitle(mailMessage.getSubject());
        dto.setContentTxt(mailMessage.getSubject());
        dto.setNotifationType(CommonConsts.NOTIFICATION_TYPE_ADMIN);
        return createNotifation(dto);
    }

    @Override
    public ApiRestResult notifyCompany(MailMessage mailMessage, Long companyId) {
        NotifationNewDto dto = new NotifationNewDto();
        dto.setTitle(mailMessage.getSubject());
        dto.setContentTxt(mailMessage.getSubject());
        dto.setNotifationType(CommonConsts.NOTIFICATION_TYPE_COMPANY);
        dto.setCompanyId(companyId);
        return createNotifation(dto);
    }
}
